﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WebService.DataAccess;using WebService.BusinessEntities;
using WebService.DataAccess.Generic;
using System.Globalization;

namespace WebService.DataAccess
{
    /// <summary>
    /// This factory class takes a CasinoUser object and generates
    /// the dbcommand object needed to call the underlying update
    /// stored proc.
    /// </summary>
    internal class CasinoUserUpdateFactory : IDbToBusinessEntityNameMapper, IUpdateFactory<CasinoUser>
    {
        /// <summary>
        /// Creates the CasinoUserUpdateFactory to build an update statement for
        /// the given CasinoUser object.
        /// </summary>
        /// <param name="CasinoUser">CasinoUser to update into the database.</param>
        public CasinoUserUpdateFactory()
        {
        }

        #region IUpdateFactory<CasinoUser> Members

        public DbCommand ConstructUpdateCommand(Database db, CasinoUser casinoUser)
        {
            DbCommand command = db.GetStoredProcCommand("dbo.UPDATE_CASINO_USER");

            db.AddInParameter(command, "avatar", DbType.Object, casinoUser.avatar);
            db.AddInParameter(command, "birth_date", DbType.DateTime, casinoUser.birth_date);
            db.AddInParameter(command, "country", DbType.String, casinoUser.country);
            db.AddInParameter(command, "email", DbType.String, casinoUser.email);
            db.AddInParameter(command, "login", DbType.String, casinoUser.login);
            db.AddInParameter(command, "nick", DbType.String, casinoUser.nick);
            db.AddInParameter(command, "user_id", DbType.Int32, casinoUser.user_id);
            db.AddInParameter(command, "user_name", DbType.String, casinoUser.user_name);
            db.AddInParameter(command, "user_surname", DbType.String, casinoUser.user_surname);
            
            return command;
        }
        #endregion

        #region IDbToBusinessEntityNameMapper Members
        /// <summary>
        /// Maps a field name in the database (usually a parameter name for a stored proc)
        /// to the corresponding business entity property name.
        /// </summary>
        /// <remarks>This method is intended for error message handling, not for reflection.</remarks>
        /// <param name="dbParameter">Name of field/parameter that the database knows about.</param>
        /// <returns>Corresponding business entity field name.</returns>
        public string MapDbParameterToBusinessEntityProperty(string dbParameter)
        {
            switch (dbParameter)
            {
                case "avatar":
                    return "avatar";
                case "birth_date":
                    return "birth_date";
                case "country":
                    return "country";
                case "email":
                    return "email";
                case "login":
                    return "login";
                case "nick":
                    return "nick";
                case "user_id":
                    return "user_id";
                case "user_name":
                    return "user_name";
                case "user_surname":
                    return "user_surname";
                default:
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter);
            }
        }
        #endregion
    }
}

